<?php

namespace app\common;

/**
 * 全部的音频文件操作方法类
 */
class AudioFileAllOperation
{
    /**
     * 将音频文件转换为wav格式
     *
     * @param [type] $input_filepath 转换前文件路径
     * @param [type] $output_filepath 转换后文件路径
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public static function moveFileandTrans2Wav($input_filepath, $output_filepath)
    {
        if (pathinfo($output_filepath)['extension'] != "wav") {
            throw new \Exception("output file extension is not wav!");
        }
        switch (strtolower(pathinfo($input_filepath)['extension'])) {
            case "pcm":
                exec("ffmpeg -y -f s16le -ar 16000 -ac 1 -i '$input_filepath' '$output_filepath'");
                break;
            case "wav":
            case "aac":
            case "m4a":
            case "amr":
            case "mp3":
                exec("ffmpeg -y -i '$input_filepath' -acodec pcm_s16le -ar 16000 -ac 1 '$output_filepath'");
                break;
            case "ogg":
            case "opus":
                exec("ffmpeg -y -i '$input_filepath' -ar 16000 -ac 1 '$output_filepath'");
                break;
            default:
                break;
        }
        if (!is_file($output_filepath)) {
            throw new \Exception("moveFileandTrans2Wav output file is failed");
        }
    }

    /**
     * 将wav文件转换为其余格式
     *
     * @param [type] $input_filepath
     * @param [type] $output_filepath
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public static function wavTrans2Another($input_filepath, $output_filepath)
    {
        if (pathinfo($input_filepath)['extension'] != "wav") {
            throw new \Exception("input file extension is not wav!");
        }
        switch (strtolower(pathinfo($output_filepath)['extension'])) {
            case "pcm":
            case "aac":
            case "m4a":
            case "amr":
            case "mp3":
                exec("ffmpeg -y -f s16le -ar 16000 -ac 1 -i '$input_filepath' '$output_filepath'");
                break;
            case "ogg":
            case "opus":
                exec("ffmpeg -y -i '$input_filepath' -ar 16000 -ac 1 '$output_filepath'");
                break;
            default:
                break;
        }
        if (!is_file($output_filepath)) {
            throw new \Exception("wavTrans2Another output file is failed");
        }
    }

    /**
     * 输入wav文件，输出转换的json文件
     *
     * @param [type] $input_wav
     * @param [type] $output_json
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public static function wave2json($input_wav, $output_json, $length)
    {
        $length = round($length * 16);
        exec("wav2json $input_wav -s $length -o $output_json");
        if (!is_file($output_json)) {
            throw new \Exception("wave2json output file is failed");
        }
        $old_json = json_decode(file_get_contents($output_json));
        foreach ($old_json->left as $key => $val) {
            $new_json[$key] = $val;
        }
        file_put_contents($output_json, json_encode($new_json));
    }


    /**
     * 获取wav文件的时长详情
     *
     * @param [type] $wav
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public static function waveDetailLength($wav)
    {

        $output = str_replace(".wav", ".sox", $wav);
        exec("sox $wav -n stat 2> $output");
        if (!is_file($output)) {
            throw new \Exception("waveDetailLength output file is failed");
        }
        $content = file_get_contents($output);
        $start_num = strpos($content, "Length (seconds):");
        $end_num = strpos($content, "Scaled by:");
        $wav_length = trim(substr($content, $start_num + 17, $end_num - strlen($content)));
        return $wav_length;
    }


    /**
     * 合并wav文件到一个临时文件路径
     *
     * @param [type] $fileGroup
     * @param [type] $output
     * @param integer $is_del 预留删除文件列表文件
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public static function mergerWaves($fileGroup, $output, $is_del = 1)
    {
        //删除原始文件
        //@unlink($output);
        $cmd = "";
        foreach ($fileGroup as $file) {
            $cmd .= "$file ";
        }
        exec("sox $cmd $output");
        if (!is_file($output)) {
            throw new \Exception("mergerWaves output file is failed");
        }
    }

    //public static function 
}
